package net.expectx.pay.gateway.rpc.service.impl;


import net.expectx.common.single.base.BaseConstants;
import net.expectx.common.single.base.BaseResult;
import net.expectx.common.single.base.BaseResultConstants;
import net.expectx.common.single.db.DataSourceEnum;
import net.expectx.common.single.db.DynamicDataSource;
import net.expectx.pay.gateway.dao.mapper.BaseMapper;
import net.expectx.pay.gateway.rpc.api.BaseService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author lijian
 */
public abstract class BaseServiceImpl<Record, Example> implements BaseService<Record, Example> {
    private static final Logger LOGGER = Logger.getLogger(net.expectx.common.single.base.BaseServiceImpl.class);
    @Autowired
    private BaseMapper<Record, Example> baseMapper;

    public BaseServiceImpl() {
    }

    public BaseResult insert(Record record) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
            boolean flag = this.baseMapper.insert(record) > 0;
            DynamicDataSource.clearDataSource();
            return !flag ? new BaseResult(Boolean.FALSE, BaseResultConstants.EXCEPTION_DATA_BASE_INSERT, (Object)null) : new BaseResult(Boolean.TRUE, BaseResultConstants.SUCCESS, record);
        } catch (Exception var3) {
            var3.printStackTrace();
            LOGGER.error("系统错误:" + var3.getLocalizedMessage());
            return new BaseResult(Boolean.FALSE.booleanValue(), BaseConstants.FAILE.intValue(), "系统错误:" + var3.getLocalizedMessage(), (Object)null);
        }
    }

    public BaseResult insertSelective(Record record) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
            boolean flag = this.baseMapper.insertSelective(record) > 0;
            DynamicDataSource.clearDataSource();
            return !flag ? new BaseResult(Boolean.FALSE, BaseResultConstants.EXCEPTION_DATA_BASE_INSERT, (Object)null) : new BaseResult(Boolean.TRUE, BaseResultConstants.SUCCESS, record);
        } catch (Exception var3) {
            var3.printStackTrace();
            LOGGER.error("系统错误:" + var3.getLocalizedMessage());
            return new BaseResult(Boolean.FALSE.booleanValue(), BaseConstants.FAILE.intValue(), "系统错误:" + var3.getLocalizedMessage(), (Object)null);
        }
    }

    public BaseResult deleteByExample(Example example) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
            this.baseMapper.deleteByExample(example);
            DynamicDataSource.clearDataSource();
            return new BaseResult(Boolean.TRUE, BaseResultConstants.SUCCESS, (Object)null);
        } catch (Exception var3) {
            var3.printStackTrace();
            LOGGER.error("系统错误:" + var3.getLocalizedMessage());
            return new BaseResult(Boolean.FALSE.booleanValue(), BaseConstants.FAILE.intValue(), "系统错误:" + var3.getLocalizedMessage(), (Object)null);
        }
    }

    public BaseResult deleteByPrimaryKey(Object id) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
            this.baseMapper.deleteByPrimaryKey(id);
            DynamicDataSource.clearDataSource();
            return new BaseResult(Boolean.TRUE, BaseResultConstants.SUCCESS, (Object)null);
        } catch (Exception var3) {
            var3.printStackTrace();
            LOGGER.error("系统错误:" + var3.getLocalizedMessage());
            return new BaseResult(Boolean.FALSE.booleanValue(), BaseConstants.FAILE.intValue(), "系统错误:" + var3.getLocalizedMessage(), (Object)null);
        }
    }

    public BaseResult updateByExampleSelective(Record record, Example example) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
            boolean flag = this.baseMapper.updateByExampleSelective(record, example) > 0;
            DynamicDataSource.clearDataSource();
            return !flag ? new BaseResult(Boolean.FALSE, BaseResultConstants.EXCEPTION_DATA_BASE_UPDATE, (Object)null) : new BaseResult(Boolean.TRUE, BaseResultConstants.SUCCESS, record);
        } catch (Exception var4) {
            var4.printStackTrace();
            LOGGER.error("系统错误:" + var4.getLocalizedMessage());
            return new BaseResult(Boolean.FALSE.booleanValue(), BaseConstants.FAILE.intValue(), "系统错误:" + var4.getLocalizedMessage(), (Object)null);
        }
    }

    public BaseResult updateByExample(Record record, Example example) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
            boolean flag = this.baseMapper.updateByExample(record, example) > 0;
            DynamicDataSource.clearDataSource();
            return !flag ? new BaseResult(Boolean.FALSE, BaseResultConstants.EXCEPTION_DATA_BASE_UPDATE, (Object)null) : new BaseResult(Boolean.TRUE, BaseResultConstants.SUCCESS, record);
        } catch (Exception var4) {
            var4.printStackTrace();
            LOGGER.error("系统错误:" + var4.getLocalizedMessage());
            return new BaseResult(Boolean.FALSE.booleanValue(), BaseConstants.FAILE.intValue(), "系统错误:" + var4.getLocalizedMessage(), (Object)null);
        }
    }

    public BaseResult updateByPrimaryKeySelective(Record record) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
            boolean flag = this.baseMapper.updateByPrimaryKeySelective(record) > 0;
            DynamicDataSource.clearDataSource();
            return !flag ? new BaseResult(Boolean.FALSE, BaseResultConstants.EXCEPTION_DATA_BASE_UPDATE, (Object)null) : new BaseResult(Boolean.TRUE, BaseResultConstants.SUCCESS, record);
        } catch (Exception var3) {
            var3.printStackTrace();
            LOGGER.error("系统错误:" + var3.getLocalizedMessage());
            return new BaseResult(Boolean.FALSE.booleanValue(), BaseConstants.FAILE.intValue(), "系统错误:" + var3.getLocalizedMessage(), (Object)null);
        }
    }

    public BaseResult updateByPrimaryKey(Record record) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
            boolean flag = this.baseMapper.updateByPrimaryKey(record) > 0;
            DynamicDataSource.clearDataSource();
            return !flag ? new BaseResult(Boolean.FALSE, BaseResultConstants.EXCEPTION_DATA_BASE_UPDATE, (Object)null) : new BaseResult(Boolean.TRUE, BaseResultConstants.SUCCESS, record);
        } catch (Exception var3) {
            var3.printStackTrace();
            LOGGER.error("系统错误:" + var3.getLocalizedMessage());
            return new BaseResult(Boolean.FALSE.booleanValue(), BaseConstants.FAILE.intValue(), "系统错误:" + var3.getLocalizedMessage(), (Object)null);
        }
    }

    public List<Record> selectByExample(Example example) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        List<Record> records = this.baseMapper.selectByExample(example);
        DynamicDataSource.clearDataSource();
        return records;
    }

    public Record selectByPrimaryKey(Object id) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        Record record = this.baseMapper.selectByPrimaryKey(id);
        DynamicDataSource.clearDataSource();
        return record;
    }

    public long countByExample(Example example) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        long count = this.baseMapper.countByExample(example);
        DynamicDataSource.clearDataSource();
        return count;
    }

    public BaseResult physicalRemoveByExample(Byte isDeleted, Example example) {
        DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
        int i = this.baseMapper.physicalRemoveByExample(isDeleted, example);
        DynamicDataSource.clearDataSource();
        if (i == 0) {
            LOGGER.error("删除成功");
            return new BaseResult(Boolean.FALSE, BaseResultConstants.EXCEPTION_DATA_BASE_DELETE, (Object)null);
        } else {
            return new BaseResult(Boolean.TRUE, BaseResultConstants.SUCCESS, (Object)null);
        }
    }
}
